package com.swind.fast.modules.app.halali.partner.service.impl;

import com.swind.fast.common.util.oConvertUtils;

import com.swind.fast.modules.app.halali.partner.vo.request.LoginVo;
import com.swind.fast.modules.app.halali.partner.vo.request.LoginRestVo;
import com.swind.fast.modules.app.halali.partner.vo.response.UserInfoVo;

import com.swind.fast.modules.app.halali.partner.entity.HalaliPartner;
import com.swind.fast.modules.app.halali.partner.mapper.HalaliPartnerMapper;
import com.swind.fast.modules.app.halali.partner.service.IHalaliPartnerService;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.ApplicationContext;

import org.apache.ibatis.annotations.Param;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import com.swind.fast.common.system.util.JwtUtil;
import com.swind.fast.common.constant.CommonConstant;
import com.swind.fast.common.util.RedisUtil;

import cn.hutool.core.date.DateBetween;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;

import javax.annotation.Resource;

import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;

/**
 * @Description: 项目
 * @Author: swind
 * @Date:   2021-11-20
 * @Version: V1.0
 */
@Slf4j
@Service
public class HalaliPartnerServiceImpl extends ServiceImpl<HalaliPartnerMapper, HalaliPartner> implements IHalaliPartnerService {
    @Autowired
    private RedisUtil redisUtil;

    public UserInfoVo login(LoginRestVo loginVo) {
        String email = loginVo.getEmail();
        HalaliPartner user = this.getUserByEmail(email);
        if (oConvertUtils.isEmpty(user)) {
            user = new HalaliPartner();
            user.setEmail(email);
            this.save(user);
        }

        UserInfoVo userInfo = new UserInfoVo();
        BeanUtils.copyProperties(user, userInfo);

        String token = JwtUtil.sign(email, email);
        // 设置token缓存有效时间
        redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
        redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME * 2 / 1000);

        userInfo.setToken(token);
        return userInfo;
    }

    public HalaliPartner getUserByMobile(String mobile) {
        if (oConvertUtils.isEmpty(mobile)) return null;

        LambdaQueryWrapper<HalaliPartner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HalaliPartner::getMobile, mobile).last("limit 1");
        HalaliPartner user = this.getOne(queryWrapper);

        return user;
    }

    public HalaliPartner getUserByEmail(String email) {
        if (oConvertUtils.isEmpty(email)) return null;

        LambdaQueryWrapper<HalaliPartner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HalaliPartner::getEmail, email).last("limit 1");
        HalaliPartner user = this.getOne(queryWrapper);
        log.info("user - 111: {}", user);

        return user;
    }

}
